package com.wanchuang.qhbb.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.view.pinnedheader.PinnedHeaderAdapter;

import java.util.List;

public class PolicyAdapter extends PinnedHeaderAdapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_ITEM_TIME = 0;
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;

    private List<PolicyPeopleBean> mDataList;
    private OnItemClickListener onItemClickListener;


    public PolicyAdapter(List<PolicyPeopleBean> dataList,OnItemClickListener onItemClickListener) {
        mDataList = dataList;
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<PolicyPeopleBean> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TIME) {
            return new TitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_policy_title, parent, false));
        } else {
            return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_policy_content, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_ITEM_TIME) {
            TitleHolder titleHolder = (TitleHolder) holder;
            titleHolder.mTextTitle.setText(mDataList.get(position).getMember_name());
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            contentHolder.mTextTitle.setText(mDataList.get(position).getMember_name() + " " +
                    mDataList.get(position).getMember_mobile());
            if (mDataList.get(position).getIs_new().equals("1"))
			{
				contentHolder.mTvNew.setVisibility(View.VISIBLE);
			}else
			{
				contentHolder.mTvNew.setVisibility(View.INVISIBLE);
			}
            holder.itemView.setOnClickListener(v -> onItemClickListener.onClick(v,mDataList.get(position).getMember_id(),
                   mDataList.get(position).getMember_name()    , mDataList.get(position).getMember_mobile(), mDataList.get(position).getAdd_time()));
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mDataList.get(position).getType() == 1) {
            return VIEW_TYPE_ITEM_TIME;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    @Override
    public boolean isPinnedPosition(int position) {
        return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        TextView mTextTitle;
        TextView mTvNew;

        ContentHolder(View itemView) {
            super(itemView);
            mTextTitle = itemView.findViewById(R.id.text_adapter_content_name);
            mTvNew = itemView.findViewById(R.id.tv_new_people);
        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {

        TextView mTextTitle;

        TitleHolder(View itemView) {
            super(itemView);
            mTextTitle = itemView.findViewById(R.id.text_adapter_title_name);
        }
    }


    /**
     * 监听输入框内容
     */
    public interface OnItemClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v,String  member_id,String name,String phone,String time);
    }

}
